//
//  LZTStatusPictureView.swift
//  LZTWeibo
//
//  Created by Yin jianxun on 16/8/14.
//  Copyright © 2016年 YinJianxun. All rights reserved.
//

import UIKit

//collectionCell的可重用标识
private let LZTStatusPictureViewCellId = "LZTStatusPictureViewCellId"

//间距
private let margin: CGFloat = 5
private let cellWH: CGFloat = 80


class LZTStatusPictureView: UICollectionView {
    
    //供外界赋值
    var pictureInfos: [LZTPictureInfo]? {
        
        didSet{
            //赋值
            contentLabel.text = "\(pictureInfos?.count ?? 0)"
            
            let size = getPictureSize(pictureInfos?.count ?? 0)
            
            //约束对应的 相对对象没有改变
            //更新约束
            self.snp_updateConstraints { (make) in
                make.size.equalTo(size)
            }
            
            //刷新
            reloadData()
        }
    }
    
    //计算配图的 size
    func getPictureSize(count: Int)-> CGSize{
        
        //行数和列数
        //4张配图时 2*2
        let col = count == 4 ? 2 : count > 3 ? 3 :count
        let row = count == 4 ? 2 : (count - 1) / 3 + 1
        
        //计算宽高
        let w = CGFloat(col) * cellWH + CGFloat(col - 1) * margin
        let h = CGFloat(row) * cellWH + CGFloat(row - 1) * margin
        
        return CGSize(width: w, height: h)
        
    }
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: UICollectionViewFlowLayout())
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
        backgroundColor = UIColor.whiteColor()
        //添加控件
        addSubview(contentLabel)
        //添加约束
        contentLabel.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        //注册cell
        registerClass(LZTStatusPictureViewCell.self, forCellWithReuseIdentifier: LZTStatusPictureViewCellId)
        //设置代理
        dataSource = self
        
        //设置flowlayout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        //设置ItemSize
        layout.itemSize = CGSize(width: cellWH, height: cellWH)
        //设置水平 垂直间距
        layout.minimumLineSpacing = margin
        layout.minimumInteritemSpacing = margin
        
    }
    
    //懒加载控件
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.redColor(), textSize: 35)
    
}

extension LZTStatusPictureView: UICollectionViewDataSource {
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return pictureInfos?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
     
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LZTStatusPictureViewCellId, forIndexPath: indexPath) as! LZTStatusPictureViewCell

        //cell的赋值
        cell.picUrl = pictureInfos![indexPath.item]
        
        return cell
    }
    
}

//自定义cell
class LZTStatusPictureViewCell: UICollectionViewCell {
    
    //供外界调用 传值
    var picUrl: LZTPictureInfo? {
        didSet{
            
            bgImageView.lzt_setImageWithString(picUrl?.thumbnail_pic, placeHolderImgName: "weibo_placeholder")
            
            if let pic = picUrl?.thumbnail_pic where pic.hasSuffix(".gif") {
                
                //显示gif(cell复用的避免)
                gifImageView.hidden = false
            } else {
                //隐藏gif(cell复用的避免)
                gifImageView.hidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        
        backgroundColor = UIColor.clearColor()
        //添加控件
        contentView.addSubview(bgImageView)
        contentView.addSubview(gifImageView)
        //添加约束
        bgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) in
            make.bottom.trailing.equalTo(contentView)
        }
    }
    
    //懒加载图片
    //背景图片
    private lazy var bgImageView: UIImageView = {
        
        let img = UIImageView(imgName: "weibo_placeholder")
        img.contentMode = .ScaleAspectFill
        img.clipsToBounds = true
        return img
    }()
    //gif
    private lazy var gifImageView: UIImageView = UIImageView(imgName: "timeline_image_gif")
}
